﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using DataLayer.Class;
using System.Data.Objects;
using System.Data;

namespace DataLayer.Database
{
    public class MusteriDB
    {

        public static ArrayList TumOncelikleriCek()
        {
            DBConnection cn = new DBConnection();
            ArrayList oncelikler = new ArrayList();
            IEnumerator<ONCELIK> num =  cn.ConnectDB.TumOncelikleriCek().GetEnumerator();

            while (num.MoveNext())
            {
                Oncelik o = new Oncelik();
                o.OncelikID = num.Current.OncelikID;
                o.Ad = num.Current.Ad;
                o.Katsayi = num.Current.Katsayi;
                o.BarajPuani = num.Current.BarajPuani;

                oncelikler.Add(o);
            }

            return oncelikler;
        }

        public void oncelikEkle(Oncelik oncelik)
        {
            DBConnection cn = new DBConnection();
            cn.ConnectDB.OncelikEkle(oncelik.Ad,oncelik.Katsayi,oncelik.BarajPuani,Session.kullaniciKodu);
            cn.ConnectDB.SaveChanges();
        }

        public void oncelikDuzenle(Oncelik oncelik)
        {
            DBConnection cn = new DBConnection();
            cn.ConnectDB.OncelikDuzenle(oncelik.OncelikID, oncelik.Katsayi, oncelik.BarajPuani,oncelik.Ad,Session.kullaniciKodu);
            cn.ConnectDB.SaveChanges();
        }

        public static DataTable onceligeSahipMusteriler(int oncelikID)
        {
            DBConnection cn = new DBConnection();
            DataTable dt = new DataTable();
            IEnumerator<OnceligeSahipMusteriler> num = cn.ConnectDB.OnceligeSahipMusteriler(oncelikID).GetEnumerator();
            dt.Columns.Add("Müşteri ID");
            dt.Columns.Add("Ad");
            dt.Columns.Add("Soyad");

            while (num.MoveNext())
                dt.Rows.Add(num.Current.MusteriID,num.Current.Ad,num.Current.Soyad);


            return dt;
           
        }

        public void oncelikSil(int oncelikID)
        {
            DBConnection cn = new DBConnection();
            cn.ConnectDB.OncelikSil(oncelikID,Session.kullaniciKodu);
            cn.ConnectDB.SaveChanges();
        }

        public static ArrayList tumMusterileriCek(string aranan, int kriter)
        {
            DBConnection cn = new DBConnection();
            IEnumerator<TumMusterileriCek_Result> num = cn.ConnectDB.TumMusterileriCek(aranan,kriter).GetEnumerator();
            ArrayList musteriler = new ArrayList();
            while (num.MoveNext())
            {
                Musteri m = new Musteri();
                m.MusteriID = num.Current.MusteriID;
                m.Ad = num.Current.MusteriAdi;
                m.Soyad = num.Current.Soyad;
                m.TelefonNo = num.Current.TelefonNo;
                m.Puan = num.Current.Puan;
                m.KayitTarihi = num.Current.KayitTarihi;
                m.Oncelik = new Oncelik(num.Current.OncelikID,num.Current.OncelikAdi,num.Current.Katsayi,num.Current.BarajPuani);

                musteriler.Add(m);

            }

            return musteriler;
        }

        public void musteriEkle(Musteri musteri)
        {
            DBConnection cn = new DBConnection();
            cn.ConnectDB.MusteriEkle(musteri.Ad,musteri.Soyad,musteri.Oncelik.OncelikID,musteri.Oncelik.BarajPuani,musteri.KayitTarihi,musteri.TelefonNo,Session.kullaniciKodu);
            cn.ConnectDB.SaveChanges();
        }


        public void musteriDuzenle(Musteri musteri)
        {
            DBConnection cn = new DBConnection();
            cn.ConnectDB.MusteriDuzenle(musteri.MusteriID,musteri.Ad, musteri.Soyad, musteri.Oncelik.OncelikID, musteri.TelefonNo,Session.kullaniciKodu);
            cn.ConnectDB.SaveChanges();
        }

        public void musteriSil(int musteriID)
        {
            DBConnection cn = new DBConnection();
            cn.ConnectDB.MusteriSil(musteriID,Session.kullaniciKodu);
            cn.ConnectDB.SaveChanges();
        }
    }
}
